package com.haoyeteng.sxbd.modular.business.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.common.constant.factory.PageFactory;
import com.haoyeteng.sxbd.core.util.DateUtil;
import com.haoyeteng.sxbd.modular.fdc.pojo.FDCDuty;
import com.haoyeteng.sxbd.modular.fdc.service.IFDCDutyService;
import com.haoyeteng.sxbd.modular.fdc.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/fdcDuty")
public class FDCDutyController extends BaseController {
    private String PREFIX = "/business/fdcDuty/";

    @Autowired
    private IFDCDutyService fdcDutyService;

    /**
     * 跳转到添加用户
     */
    @RequestMapping("")
    public String FDCDuty() {
        return PREFIX + "duty.html";
    }



    /**
     * 获取用户列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    // @Permission
    public ResponseResult<Object> list(String dutyName) {
        ResponseResult<Object> result = new ResponseResult<Object>();
        Page<FDCDuty> page = new PageFactory<FDCDuty>().defaultPage();
//        Map<String, Object> params = new HashMap<>(16);

//        params.put("name",dutyName);
        List<Map<String, Object>> fdcDutyList = fdcDutyService.selectDuty(page,dutyName);
        result.setData(fdcDutyList);
        result.setCount(page.getTotal());
        result.setCode(SUCCESS_TIP.getCode());
        result.setMsg(SUCCESS_TIP.getMessage());
        return result;
    }

    /**
     *跳转页面新增部门
     */
    @RequestMapping("/duty_add")
    public String DutyAdd() {
        return PREFIX + "duty_add.html";
    }

    /**
     * 新增部门
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(FDCDuty fdcDuty) {
        fdcDutyService.insertFdcDuty(fdcDuty);
        return SUCCESS_TIP;
    }
    /**
     * 删除用户
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam long id) {
        fdcDutyService.deleteById(id);
        return SUCCESS_TIP;
    }


    /**
     * 跳转到修改部门信息
     */
    @RequestMapping("/updateDutyById/{id}")
    public String updateUserById(@PathVariable long id, Model model) {
        FDCDuty fdcDuty = fdcDutyService.selectById(id);
        String createDay = DateUtil.getDay(fdcDuty.getCreateTime());
        String updateDay = DateUtil.getDay(fdcDuty.getUpdateTime());
        model.addAttribute("item",fdcDuty);
        model.addAttribute("createDay",createDay);
        model.addAttribute("updateDay",updateDay);
        return PREFIX + "updateDuty.html";
    }
    /**
     * 修改部门信息
     */
    @RequestMapping(value = "/updateDuty")
    @ResponseBody
    public Object updateDuty(FDCDuty fdcDuty) {
        fdcDuty.setUpdateTime(new Date());
        fdcDutyService.updateById(fdcDuty);
        return SUCCESS_TIP;
    }

    /**
     * 部门导出本地excel模板
     */
    @RequestMapping(value = "/downloadExample", method = RequestMethod.POST)
    public void downloadExample(HttpServletResponse response) throws IOException {
        // 获取固定Excel文件作为输入流 (参数为配置的模板路径)
        InputStream in = this.getClass().getResourceAsStream("/statics/admin_local_usergroup.xls");
        OutputStream toClient = null;
        byte[] buffer;
        try {
            // 获取导出文件的字节数组
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[1024];
            int len = 0;
            while ((len = in.read(buffer1)) != -1) {
                outStream.write(buffer1, 0, len);
            }
            in.close();
            buffer = outStream.toByteArray();

            // 导出Excel
            if (null != buffer && buffer.length > 0) {
                // 清空response
                response.reset();
                // 设置response的Header
                response.addHeader("Content-Disposition",
                        "attachment;filename=" + new String(("部门导入模板.xlsx").getBytes("GBK"), "ISO8859_1"));
                response.addHeader("Content-Length", "" + buffer.length);
                toClient = response.getOutputStream();
                response.setContentType("application/octet-stream");
                toClient.write(buffer);
                toClient.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(toClient != null){
                toClient.close();
            }
            if(in != null){
                in.close();
            }
        }
    }
    /**
     * 数据导出
     */
    @RequestMapping("exportAll")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<FDCDuty> usersList = fdcDutyService.selectList(new EntityWrapper<FDCDuty>());

        ExcelUtils.exportExcelToTarget(response, null, usersList, FDCDuty.class);
    }



}
